package com.sinopec.basemodule.adapter

import com.sinopec.basemodule.model.ImageBean
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import com.sinopec.basemodule.view.PhotoPicker
import com.sinopec.basemodule.R
import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.widget.ImageView
import com.sinopec.basemodule.utils.AndroidLifecycleUtils
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.Glide
import java.util.ArrayList

class PhotoPickerAdapter(
    private val context: Context,

    private val images: ArrayList<ImageBean>,
    private val listener: OnItemClickListener
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private val inflater: LayoutInflater
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        var holder: RecyclerView.ViewHolder? = null
        holder = when (viewType) {
            PhotoPicker.TYPE_ADD -> AddViewHolder(
                inflater.inflate(R.layout.item_image_add, parent, false)
            )
            else -> PhotoViewHolder(
                inflater.inflate(
                    R.layout.item_image,
                    parent,
                    false
                )
            )
        }
        return holder
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (images[position].type == PhotoPicker.TYPE_ADD) {
            val addViewHolder = holder as AddViewHolder
            addViewHolder.itemView.setOnClickListener { v: View? -> listener.itemClick(position) }
        } else {
            val photoViewHolder = holder as PhotoViewHolder
            val uri = images[position].imageUri
            if (uri == null) {
                photoViewHolder.delete.visibility = View.GONE
                return
            }
            photoViewHolder.delete.visibility = View.VISIBLE
            val canLoadImage = AndroidLifecycleUtils.canLoadImage(photoViewHolder.ivPhoto.context)
            if (canLoadImage) {
                val options = RequestOptions()
                options.centerCrop()
                    .placeholder(R.drawable.ic_rectangle_picture_loading)
                    .error(R.drawable.ic_rectangle_picture_error)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .skipMemoryCache(true)
                Glide.with(context)
                    .load(uri)
                    .apply(options)
                    .thumbnail(0.1f)
                    .into(photoViewHolder.ivPhoto)
            }
            photoViewHolder.itemView.setOnClickListener { v: View? -> listener.itemClick(position) }
            photoViewHolder.delete.setOnClickListener { v: View? -> listener.deleteClick(position) }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return images[position].type
    }

    override fun getItemCount(): Int {
        return images.size
    }

    class PhotoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val ivPhoto: ImageView
        val delete: View

        init {
            ivPhoto = itemView.findViewById(R.id.iv_photo)
            delete = itemView.findViewById(R.id.iv_delete)
        }
    }

    class AddViewHolder(itemView: View?) : RecyclerView.ViewHolder(
        itemView!!
    )

    interface OnItemClickListener {
        fun deleteClick(position: Int)
        fun itemClick(position: Int)
    }

    init {
        inflater = LayoutInflater.from(context)
    }
}